﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ex2.SumOfInfiniteSeries
{
    class SumOfInfiniteSeries
    {
        public delegate void CalculateSeries();

        static void Sum1()
        {
            double previousRes = 0;
            double currentRes = 1;
            double devider = 2;
            while (currentRes - previousRes >= 0.01)
            {
                previousRes = currentRes;
                currentRes += (double)1 / devider;
                devider *= 2;
            }
            Console.WriteLine(currentRes);
        }

        static void Sum2()
        {
            double previousRes = 0;
            double currentRes = 1;
            double devider = 2;
            while (currentRes - previousRes >= 0.01)
            {
                previousRes = currentRes;
                currentRes += (double)1 / Fact(devider);
                devider += 1;
            }
            Console.WriteLine(currentRes);
        }

        static void Sum3()
        {
            double previousRes = 0;
            double currentRes = 1;
            double devider = 2;
            int i=0;
            while (currentRes - previousRes >= 0.01)
            {
                previousRes = currentRes;
                currentRes += ((double)1 / devider)*(Math.Pow(-1,i+1));
                devider *= 2;
            }
            Console.WriteLine(currentRes);
        }

        private static double Fact(double devider)
        {
            double res = 1;
            while (devider > 0)
            {
                res *= devider;
                devider--;
            }
            return res;
        }

        static void Main(string[] args)
        {
            Sum1();
            Sum2();
            Sum3();
        }
    }
}
